package view;

import model.filemanager.MediaFile;


public class MediaFormatter {
	
	public static String FormatString(MediaFile file, String propName, MediaFormatTypeEnum formatType )
	{
		String result = "";
		switch(formatType)
		{
		case DELETE_LINK:
			result = String.format("<a href=\"/deletefile?key=%s\">Delete</a>", file.getKey());
			break;
		case DOWNLOAD_LINK:
			result = String.format("<a href=\"/downloadfile?download=yes&key=%s\">%s</a>", file.getKey(), file.getName());
			break;
		case IMG80_POPUP:
			result = String.format("<a href='#'   onclick=\"%s; return false;\"><img class='draggable' title=\"%s\" width=\"80px\"; height=\"80px\"; src=\"%s\"/></a>",
					"showPopup('" + file.getType() + "','"+file.getKey()+"',this)", file.getKey(), file.getImageUrl(80, false));
			break;
		case FILE_SIZE:	
			result = FormatFileSize(file.getFileSize());
			break;
		default:
			result = String.format("%s",file.getProperty(propName));
			break;
		}
		return result;
	}
	
	public static String FormatFileSize(long size)
	{
		double sizeD = (double)size;
		String symbol = "B";
		//1000 is standard SI unit, as opposed to MS style 1024
		if (size >= 1000.0)
		{
			symbol = "kB";
			sizeD = sizeD/1000.0;
			if (sizeD >= 1000)
			{
				symbol ="mB";
				sizeD = sizeD/1000.0;
			}
		}
		return String.format("%.2f %s", sizeD, symbol);
	}
	
}
